import { http } from '@/utils/request'

// 用户认证相关
export const authApi = {
  // 登录
  login: (data) => http.post('/auth/login', data),
  // 注册
  register: (data) => http.post('/auth/register', data),
  // 发送验证码
  sendCode: (data) => http.post('/auth/sendCode', data),
  // 重置密码
  resetPassword: (data) => http.post('/auth/resetPassword', data),
  // 验证token
  validateToken: () => http.get('/auth/validate'),
  // 退出登录
  logout: () => http.post('/auth/logout')
}

// 用户信息相关
export const userApi = {
  // 获取用户信息
  getProfile: () => http.get('/user/profile'),
  // 更新用户信息
  updateProfile: (data) => http.put('/user/profile', data),
  // 上传头像
  uploadAvatar: (data) => http.post('/user/avatar', data),
  // 获取用户简历
  getResume: () => http.get('/user/resume'),
  // 更新简历
  updateResume: (data) => http.put('/user/resume', data)
}

// 职位相关
export const jobApi = {
  // 获取职位列表
  getList: (params) => http.get('/jobs', { params }),
  // 获取职位详情
  getDetail: (id) => http.get(`/jobs/${id}`),
  // 获取职位分类
  getCategories: () => http.get('/jobs/categories'),
  // 获取热门职位
  getHotJobs: () => http.get('/jobs/hot'),
  // 搜索职位
  search: (params) => http.get('/jobs/search', { params }),
  // 收藏职位
  favorite: (id) => http.post(`/jobs/${id}/favorite`),
  // 取消收藏
  unfavorite: (id) => http.delete(`/jobs/${id}/favorite`),
  // 获取收藏列表
  getFavorites: () => http.get('/jobs/favorites'),
  // 新增：根据技能搜索
  searchBySkill: (skill, params) => http.get('/jobs/searchBySkill', { params: { skill, ...params } }),
  // 新增：根据筛选条件搜索
  searchByFilter: (filterType, params) => http.get('/jobs/searchByFilter', { params: { filterType, ...params } }),
  // 新增：获取附近职位
  getNearbyJobs: (latitude, longitude, radius = 5000) => 
    http.get('/jobs/nearby', { params: { latitude, longitude, radius } }),
  // 新增：获取推荐职位
  getRecommendJobs: (params) => http.get('/jobs/recommend', { params }),
  // 新增：获取最新职位
  getLatestJobs: (params) => http.get('/jobs/latest', { params })
}

// 简历投递相关
export const applicationApi = {
  // 投递简历
  apply: (data) => http.post('/applications', data),
  // 获取投递记录
  getList: (params) => http.get('/applications', { params }),
  // 获取投递详情
  getDetail: (id) => http.get(`/applications/${id}`),
  // 撤回投递
  withdraw: (id) => http.delete(`/applications/${id}`)
}

// 聊天相关
export const chatApi = {
  // 获取会话列表
  getConversations: () => http.get('/chat/conversations'),
  // 创建会话
  createConversation: (data) => http.post('/chat/conversation', data),
  // 获取聊天记录
  getMessages: (conversationId, params) => http.get(`/chat/messages/${conversationId}`, { params }),
  // 发送消息
  sendMessage: (data) => http.post('/chat/message', data),
  // 标记已读
  markAsRead: (messageId) => http.put(`/chat/message/${messageId}/read`),
  // 撤回消息
  recallMessage: (messageId) => http.put(`/chat/message/${messageId}/recall`),
  // 设置置顶
  pinConversation: (conversationId, isPinned) => http.put(`/chat/conversation/${conversationId}/pin?isPinned=${isPinned}`),
  // 设置免打扰
  muteConversation: (conversationId, isMuted) => http.put(`/chat/conversation/${conversationId}/mute?isMuted=${isMuted}`)
}

// 文件上传相关
export const fileApi = {
  // 上传图片
  uploadImage: (filePath) => {
    return new Promise((resolve, reject) => {
      uni.uploadFile({
        url: http.defaults.baseURL + '/file/upload/image',
        filePath,
        name: 'file',
        header: {
          'Authorization': 'Bearer ' + uni.getStorageSync('token')
        },
        success: (res) => {
          const data = JSON.parse(res.data)
          if (data.code === 0) {
            resolve(data)
          } else {
            reject(data)
          }
        },
        fail: reject
      })
    })
  },
  
  // 上传文件
  uploadFile: (filePath) => {
    return new Promise((resolve, reject) => {
      uni.uploadFile({
        url: http.defaults.baseURL + '/file/upload',
        filePath,
        name: 'file',
        header: {
          'Authorization': 'Bearer ' + uni.getStorageSync('token')
        },
        success: (res) => {
          const data = JSON.parse(res.data)
          if (data.code === 0) {
            resolve(data)
          } else {
            reject(data)
          }
        },
        fail: reject
      })
    })
  }
}

// 公共接口
export const commonApi = {
  // 获取城市列表
  getCities: () => http.get('/common/cities'),
  // 获取行业列表
  getIndustries: () => http.get('/common/industries'),
  // 获取学历列表
  getEducations: () => http.get('/common/educations'),
  // 获取工作经验列表
  getExperiences: () => http.get('/common/experiences'),
  // 获取系统配置
  getConfig: () => http.get('/common/config')
}

// 新增：公司相关
export const companyApi = {
  // 获取公司详情
  getDetail: (id) => http.get(`/companies/${id}`),
  // 获取推荐公司
  getRecommendCompanies: () => http.get('/companies/recommend'),
  // 获取公司列表
  getList: (params) => http.get('/companies', { params }),
  // 搜索公司
  search: (params) => http.get('/companies/search', { params })
}

// 导出所有API
export default {
  auth: authApi,
  user: userApi,
  job: jobApi,
  application: applicationApi,
  chat: chatApi,
  file: fileApi,
  common: commonApi,
  company: companyApi
}